package com.ruoyi;

import com.ruoyi.courses.domain.Courses;
import com.ruoyi.courses.service.ICoursesService;
import com.ruoyi.menuOrders.mapper.MenuOrdersMapper;
import com.ruoyi.orders.domain.Orders;
import com.ruoyi.orders.mapper.OrdersMapper;
import com.ruoyi.orders.service.IOrdersService;
import com.ruoyi.outuser.domain.OutUser;
import com.ruoyi.outuser.service.IOutUserService;
import com.ruoyi.place.domain.Place;
import com.ruoyi.place.service.IPlaceService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;

@RestController
@RequestMapping("/info/info")
public class InfoPageController {
    @Resource
    IOutUserService outUserService;
    @Resource
    IPlaceService placeService;
    @Resource
    ICoursesService coursesService;
    @Resource
    IOrdersService ordersService;
    @Resource
    OrdersMapper ordersMapper;
    @Resource
    MenuOrdersMapper menuOrdersMapper;
    @GetMapping("/getInfo")
    public Map<String, Object> getHualiByState() {

        Map<String, Object> map=new HashMap<>();

        List<OutUser> outUsers = outUserService.selectOutUserList(new OutUser());
        map.put("userCount", outUsers.size());
        List<Place> places = placeService.selectPlaceList(new Place());
        map.put("goodsCount",places.size());
        List<Courses> courses = coursesService.selectCoursesList(new Courses());
        map.put("orderCount",courses.size());
        Orders orders1 = new Orders();
        LocalDate now = LocalDate.now();
        // 创建一个 DateTimeFormatter 实例
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化日期
        String formattedDate = now.format(formatter);
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("beginStartDate",formattedDate);
        stringObjectMap.put("endStartDate",formattedDate);
        orders1.setParams(stringObjectMap);
        List<Orders> orders = ordersService.selectOrdersList(orders1);
        map.put("reminderCount",orders.size());
        int i = ordersMapper.selectYuCount(formattedDate, 0);
        int i1 = ordersMapper.selectYuCount(formattedDate, 1);
        List<Integer> list=new ArrayList<>();
        list.add(i);
        list.add(i1);
        map.put("isYuList",list);
        List<Integer> list2=new ArrayList<>();
        List<String> list2N=new ArrayList<>();
//        2022-10-01 14:32:00
        for (String[] strings : getYearTimeToList(5)) {
            System.out.println(strings[0]+"  "+strings[1]);
            int a = menuOrdersMapper.getTotalProfitAndPublishDate(strings[0], strings[1]);
            int b = ordersMapper.getTotalProfitAndPublishDate(strings[0], strings[1]);
            list2N.add(strings[0].substring(0,4));
            list2.add(a+b);
        }
        map.put("nianNameList",list2N);
        map.put("nianList",list2);

        List<Integer> list3=new ArrayList<>();
        List<String> list3N=new ArrayList<>();
        for (String[] strings : getMonthProfitTimeToList(12)) {
            int a = menuOrdersMapper.getTotalProfitAndPublishDate(strings[0], strings[1]);
            int b = ordersMapper.getTotalProfitAndPublishDate(strings[0], strings[1]);
            list3N.add(strings[0].substring(0,7));
            list3.add(a+b);
        }
        map.put("yueNameList",list3N);
        map.put("yueList",list3);

        List<Integer> list4=new ArrayList<>();
        List<String> list4N=new ArrayList<>();
        for (String[] strings : getTimeToList(7)) {
            int a = menuOrdersMapper.getTotalProfitAndPublishDate(strings[0], strings[1]);
            int b = ordersMapper.getTotalProfitAndPublishDate(strings[0], strings[1]);
            list4N.add(strings[0].substring(0,10));
            list4.add(a+b);
        }
        map.put("riNameList",list4N);
        map.put("riList",list4);
        return map;
    }
    public  static List<String[]> getTimeToList(Integer num){
        // 获取当前的 LocalDateTime
        LocalDateTime now = LocalDateTime.now();

        // 创建一个 DateTimeFormatter 实例
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 创建一个列表来存储日期时间范围
        List<String[]> dateTimeRanges = new ArrayList<>();

        // 循环生成日期时间范围
        for (int i = 0; i < num; i++) {
            // 计算前一天的 LocalDateTime
            LocalDateTime startDateTime = now.minusDays(i).truncatedTo(ChronoUnit.DAYS);
            LocalDateTime endDateTime = startDateTime.plusDays(1);

            // 格式化日期时间
            String startDateTimeStr = startDateTime.format(formatter);
            String endDateTimeStr = endDateTime.format(formatter);

            // 添加日期时间范围到列表
            dateTimeRanges.add(new String[]{startDateTimeStr, endDateTimeStr});
        }
        return dateTimeRanges;
    }
    public static List<String[]> getYearTimeToList(Integer num){
        // 获取当前的 LocalDateTime
        LocalDateTime now = LocalDateTime.now();

        // 创建一个 DateTimeFormatter 实例
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 创建一个列表来存储日期时间范围
        List<String[]> yearTimeRanges = new ArrayList<>();

        // 循环生成日期时间范围
        for (int i = 0; i < num; i++) {
            // 计算前一年的 LocalDateTime
            LocalDateTime startYearDateTime = now.minusYears(i).withMonth(1).withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS);
            LocalDateTime endYearDateTime = startYearDateTime.plusYears(1);

            // 格式化日期时间
            String startYearDateTimeStr = startYearDateTime.format(formatter);
            String endYearDateTimeStr = endYearDateTime.format(formatter);

            // 添加日期时间范围到列表
            yearTimeRanges.add(new String[]{startYearDateTimeStr, endYearDateTimeStr});
        }

        return yearTimeRanges;
    }
    public static List<String[]> getMonthProfitTimeToList(Integer num){
        // 获取当前的 LocalDate
        LocalDate now = LocalDate.now();

        // 创建一个 DateTimeFormatter 实例
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 创建一个列表来存储日期时间范围
        List<String[]> monthProfitRanges = new ArrayList<>();

        // 循环生成日期时间范围
        for (int i = 0; i < num; i++) {
            // 计算前i个月的 LocalDate
            LocalDate startMonthDate = now.minusMonths(i).withDayOfMonth(1);
            LocalDate endMonthDate = startMonthDate.plusMonths(1);

            // 将 LocalDate 转换为 LocalDateTime 并格式化
            LocalDateTime startMonthDateTime = startMonthDate.atStartOfDay();
            LocalDateTime endMonthDateTime = endMonthDate.atStartOfDay();

            // 格式化日期时间
            String startMonthDateTimeStr = startMonthDateTime.format(formatter);
            String endMonthDateTimeStr = endMonthDateTime.format(formatter);

            // 添加日期时间范围到列表
            monthProfitRanges.add(new String[]{startMonthDateTimeStr, endMonthDateTimeStr});
        }

        return monthProfitRanges;
    }
}
